<?php

/*
 * Following code will create a new product row
 * All product details are read from HTTP Post Request
 */

// array for JSON response
$response = array();

// check for required fields
if (isset($_POST['user_id']) && isset($_POST['name'])&& isset($_POST['avatar'])) {
    
    $user_id = $_POST['user_id'];
    $name = $_POST['name'];
	$avatar = $_POST['avatar'];

    // include db connect class
    require_once __DIR__ . '/db_connect.php';

    // connecting to db
    $db = new DB_CONNECT();

    // mysql inserting a new row
	$check = mysql_query("SELECT *FROM user WHERE user_id = $user_id");
	if(mysql_fetch_array($check) !== false)
	{
        //update 
    $result = mysql_query("UPDATE user SET name = '$name' , avatar = '$avatar' WHERE user_id = $user_id");

    //get message by id receive
    $resultMessage =$result = mysql_query("SELECT id_send,id_receive,is_accept,id_request,name,message.id FROM message,user WHERE id_receive = $user_id AND user_id = $user_id ") or die(mysql_error());
    if (mysql_num_rows($result) > 0) {
    // looping through all results
    // game node
    $response["message"] = array();
    
    while ($row = mysql_fetch_array($result)) {
        // temp user array
        $message = array();
        $message["id"] = $row["id"];
        $message["id_send"] = $row["id_send"];
        $message["id_receive"] = $row["id_receive"];
        $message["is_acept"] = $row["is_accept"];
        $message["id_request"] = $row["id_request"];
        $message["name"] = $row["name"];


        // push single product into final response array
        array_push($response["message"], $message);
    }


    // success
 //   $response["responseCode"] = 1;

    // echoing JSON response
   // echo json_encode($response);
}

	}
	else{
	$result = mysql_query("INSERT INTO user(user_id, name, avatar) VALUES('$user_id', '$name', '$avatar')");
	}

    // check if row inserted or not
    if ($result) {
        // successfully inserted into database
        $result = mysql_query("SELECT * FROM game WHERE user_id = $user_id ") or die(mysql_error());
    $response["game"] = array();
        $game = array();

// check for empty result
if (mysql_num_rows($result) > 0) {
    // looping through all results
    // game node
    
    while ($row = mysql_fetch_array($result)) {
        // temp user array
        $game = array();
        $game["id"] = $row["id"];
        $game["user_id"] = $row["user_id"];
        $game["id_level"] = $row["id_level"];
        $game["rate"] = $row["rate"];
        $game["score"] = $row["score"];


        // push single product into final response array
    }
    // success

    // echoing JSON response
}
        array_push($response["game"], $game);

        $response["responseCode"] = 1;
        $response["responseMessage"] = "Successfully created.";

        // echoing JSON response
        echo json_encode($response);
    } else {
        // failed to insert row
        $response["responseCode"] = -1;
        $response["responseMessage"] = "Oops! An error occurred.";
        
        // echoing JSON response
        echo json_encode($response);
    }
} else {
    // required field is missing
    $response["responseCode"] = -1;
    $response["responseMessage"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}
?>